/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.aruoc.sim.siguelineas;

import org.aruoc.sim.SimContacto;
import org.aruoc.sim.SimWorldPainter;
import org.aruoc.sim.SimWorld;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import net.phys2d.raw.shapes.Circle;
import net.phys2d.raw.shapes.DynamicShape;
import net.phys2d.raw.shapes.Shape;

/**
 *
 * @mperezma
 */
public class LineSensor extends SimContacto {

    public int valorSensor = 0;

    /**
     * Create a new un-named body
     * 
     * @param shape The shape describing this body
     * @param m The mass of the body
     */
    public LineSensor(DynamicShape shape, float m) {
        super(shape, m, 0f);
    }

    /**
     * Create a new un-named body
     * 
     * @param shape The shape describing this body
     * @param m The mass of the body
     */
    protected LineSensor(Shape shape, float m) {
        super(shape, m, 0f);
    }

    /**
     * Create a named body
     * 
     * @param name The name to assign to the body
     * @param shape The shape describing this body
     * @param m The mass of the body
     */
    public LineSensor(String name, DynamicShape shape, float m) {
        // TODO: ¿los sensores rozan con el suelo?
        super(name, shape, m, 0f);
    }

    /**
     * Create a named body
     * 
     * @param name The name to assign to the body
     * @param shape The shape describing this body
     * @param m The mass of the body
     */
    protected LineSensor(String name, Shape shape, float m) {
        // TODO: ¿los sensores rozan con el suelo?
        super(name, shape, m, 0f);
    }

    @Override
    public void step(SimWorld world) {
        BufferedImage imagen = world.getImagen();
        // TODO convertir a entero sin instanciar floats
        try {
        valorSensor = imagen.getRGB(new Float(getPosition().getX()).intValue(), new Float(getPosition().getY()).intValue());
        }
        catch(Exception e) {
            
        }


    }

    public boolean isActivo() {
        return valorSensor != -1;
    }
    
    @Override
    public void paint(SimWorldPainter painter, Graphics2D g) {
        if (valorSensor == -1) {
            painter.drawFilledCircleBody(g, this, new Circle(2), Color.GREEN);
        } else {
            painter.drawFilledCircleBody(g, this, new Circle(13), Color.YELLOW);
            painter.drawFilledCircleBody(g, this, new Circle(3), Color.RED);
        }
    }
}
